package com.taylor.community.config;

import com.taylor.community.quartz.AlphaJob;
import com.taylor.community.quartz.PostScoreRefreshJob;
import com.taylor.community.quartz.WkImageDeleteJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * 配置任务的相关信息
 */
@Configuration
//该配置仅在第一次被读取 然后初始化到数据库 之后quartz访问数据库获取相关的配置
public class QuartzConfig {
    // 配置JobDetail
    //FactoryBean简化了Bean实例化过程
//    @Bean//初始化JobDetailFactoryBean比初始化JobDetail容易
    public JobDetailFactoryBean alphaJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        //声明要管理那个bean
        factoryBean.setJobClass(AlphaJob.class);
        factoryBean.setName("alphaJob");
        factoryBean.setGroup("alphaJobGroup");
        factoryBean.setDurability(true);//任务持久保存
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }
    // 配置Trigger
//    @Bean
    //将来JobDetailFactoryBean、JobDetail会有多个实例 Spring会根据两者同名的优先注入（根据名称进行区分）
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(alphaJobDetail);
        factoryBean.setName("alphaTrigger");
        factoryBean.setGroup("alphaTriggerGroup");
        factoryBean.setRepeatInterval(3000);//多长时间执行一次
        factoryBean.setJobDataMap(new JobDataMap());//Trigger底层要存储job的状态 用默认的JobDataMap来存储
        return factoryBean;
    }

    // 配置JobDetail
    //刷新帖子分数的任务
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail(){
        JobDetailFactoryBean bean = new JobDetailFactoryBean();
        bean.setJobClass(PostScoreRefreshJob.class);
        bean.setName("postScoreRefreshJob");
        bean.setGroup("communityJobGroup");
        //任务持久保存
        bean.setDurability(true);
        //假设应用程序出现问题 任务可恢复
        bean.setRequestsRecovery(true);
        return bean;
    }
    // 配置Trigger(触发器)
    @Bean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail  postScoreRefreshJobDetail){
        SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
        bean.setJobDetail(postScoreRefreshJobDetail);
        bean.setName("postScoreRefreshTrigger");
        bean.setGroup("communityTriggerGroup");
        bean.setRepeatInterval(1000*60*5);//五分钟执行一次
        bean.setJobDataMap(new JobDataMap());
        return bean;
    }
    // 配置JobDetail
    //删除由Wk分享产生的长图
    @Bean
    public JobDetailFactoryBean wkImageDeleteJobDetail(){
        JobDetailFactoryBean bean = new JobDetailFactoryBean();
        bean.setJobClass(WkImageDeleteJob.class);
        bean.setName("wkImageDeleteJob");
        bean.setGroup("communityJobGroup");
        //任务持久保存
        bean.setDurability(true);
        //假设应用程序出现问题 任务可恢复
        bean.setRequestsRecovery(true);
        return bean;
    }
    // 配置Trigger(触发器)
    @Bean
    public SimpleTriggerFactoryBean wkImageDeleteTrigger(JobDetail wkImageDeleteJobDetail){
        SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
        bean.setJobDetail(wkImageDeleteJobDetail);
        bean.setName("wkImageDeleteTrigger");
        bean.setGroup("communityTriggerGroup");
        bean.setRepeatInterval(1000*60*4);//四分钟执行一次
        bean.setJobDataMap(new JobDataMap());
        return bean;
    }
}
